package com.jnu.mydiary;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.jnu.mydiary.adapter.ItemAdapter;
import com.jnu.mydiary.griditem.GridItems;
import com.jnu.mydiary.ui.shouye1.DashboardFragment;
import com.jnu.mydiary.ui.shouye1.DataBank;
import com.jnu.mydiary.ui.shouye1.myBean;
import com.jnu.mydiary.ui.users.EditUserActivity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

public class AddActivity extends AppCompatActivity {

    private GridView mGridView;
    private GridView mGridView_mood;
    private List<GridItems> MoodList;
    private List<GridItems> provinceBeanList;
    private ItemAdapter mItemAdapter;
    public static String[] itemNames_mood = new String[]{"Shy","Cry","Shock","Sleepy","Joy","Happy","Sick","Angry","Bald","Sorrow","NoBB","Soso"};
    public String[] itemNames_Wheather =new String[]{"Rainbow","Thunder","Rainy","Low temp","Cloudy","High temp","Windy","Sunny","Snowy","Smog","Dust","Haze"};
    private int[] itemImage = new int[]{R.drawable.xianxingcaihong,R.drawable.xianxingleizhenyu,R.drawable.xianxingdayu,R.drawable.xianxingdiwen,R.drawable.xianxingduoyun,R.drawable.xianxinggaowen,R.drawable.xianxingdafeng,R.drawable.xianxingqingtian,R.drawable.xianxingtedabaoxue,R.drawable.xianxingwu,R.drawable.xianxingyangsha,R.drawable.xianxingyintian};
    public static int[] itemImage_mood=new int[]{R.drawable.mood_haixiu,R.drawable.mood_ku,R.drawable.mood_jingdaile,R.drawable.mood_kun,R.drawable.mood_kuaile,R.drawable.mood_kaixin,R.drawable.mood_nanshou,R.drawable.mood_shengqi,R.drawable.mood_toutu,R.drawable.mood_weiqu,R.drawable.mood_wuyu,R.drawable.mood_yiban};
    private String[] itemNames_dialy = new String[]{"Movie","Think","Drink","Beauty","Fitness","Count","Pet","Run","Emo.","Photo","Tidy up","Read"};
    private int[] itemImage_dialy=new int[]{R.drawable.icon_dianying,R.drawable.icon_fansizij,R.drawable.icon_hehsui,R.drawable.icon_hufu,R.drawable.icon_jianshen,R.drawable.icon_jizhang,
            R.drawable.icon_mao,R.drawable.icon_pao_bu,R.drawable.icon_qingxuguanli,
            R.drawable.icon_sheying,R.drawable.icon_shouna,R.drawable.icon_yuedu};
    private String mDate;
    private String mtitle;
    private String mText;
    private int id_weather;
    private int id_mood;
    private String tv_mood;
    private TextView item_date;
    private FloatingActionButton floatingActionButton;
    private GridView mGridView_dialy;
    private List<GridItems> DialyList;
    private LinearLayout mLinearLayout_dialy;
    private ArrayList<Integer> mDialy=new ArrayList<Integer>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        item_date=findViewById(R.id.home_item_tv_date);
        id_weather=R.drawable.xianxingcaihong;
        id_mood=R.drawable.mood_kaixin;
        mLinearLayout_dialy=findViewById(R.id.home_item_iv_daily);
        mtitle="无标题";
        getDate();
        item_date.setText(mDate);
        if(getIntent()!=null&&getIntent().getExtras().getString("type").equals("daka")){
            Bundle bundle = getIntent().getExtras();
            String date = bundle.getString("date");
            mDate=date;
            String[] a=date.split("-");
            mDay=Integer.parseInt(a[2]);
            item_date.setText(date);
            String mood=bundle.getString("mood");
            for(int k=0;k<itemNames_mood.length;k++)
            {
                if(itemNames_mood[k].equals(mood)){
                   id_mood=itemImage_mood[k];
                    break;
                }
            }
            ImageView iv_mood = findViewById(R.id.iv_mood);
            iv_mood.setImageResource(id_mood);
        }
        initGridView();

        floatingActionButton=findViewById(R.id.floatingActionButton);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText i=findViewById(R.id.home_item_tv_title);
                mtitle=i.getText().toString();
                EditText j=findViewById(R.id.home_item_tv_text);
                for(int l=0;l<itemImage_mood.length;l++){
                    if(itemImage_mood[l]==id_mood)
                        tv_mood=itemNames_mood[l];
                }
                mText=j.getText().toString();
                myBean mData=new myBean(mDate,mtitle,id_weather,id_mood,mText,mDialy,tv_mood);
                Intent intent=new Intent(AddActivity.this,MainActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("DataInfo", mData);
                    bundle.putString("type", "add");
                    bundle.putString("mood",tv_mood);
                    bundle.putInt("now_day",mDay);
                    intent.putExtras(bundle);
                    DashboardFragment.num=-1;
                    startActivity(intent);
                    finish();
            }
        });



    }
    private int mDay;//记录选择的是几号
    public void getDate(){
        Calendar cd = new GregorianCalendar();
        mDay=cd.get(Calendar.DAY_OF_MONTH);
        mDate=(cd.get(Calendar.YEAR)+"-"+(cd.get(Calendar.MONTH)+1)+"-"+cd.get(Calendar.DAY_OF_MONTH));

    }

    public void initGridView() {
        mGridView_mood=findViewById(R.id.gv_mood);
        MoodList = new ArrayList<>();
        for (int i = 0; i < itemNames_mood.length; i++) {
            GridItems mItemsType = new GridItems(itemNames_mood[i],itemImage_mood[i]);
            MoodList.add(mItemsType);
        }
        mItemAdapter = new ItemAdapter(this, MoodList);
        mGridView_mood.setAdapter(mItemAdapter);

        mGridView = findViewById(R.id.gv_weather);
        provinceBeanList = new ArrayList<>();
        for (int i = 0; i < itemNames_Wheather.length; i++) {
            GridItems mItemsType = new GridItems(itemNames_Wheather[i],itemImage[i]);
            provinceBeanList.add(mItemsType);
        }
        mItemAdapter = new ItemAdapter(this, provinceBeanList);
        mGridView.setAdapter(mItemAdapter);


        mGridView_dialy = findViewById(R.id.gv_dialy);
        DialyList = new ArrayList<>();
        for (int i = 0; i < itemNames_dialy.length; i++) {
            GridItems mItemsType = new GridItems(itemNames_dialy[i],itemImage_dialy[i]);
            DialyList.add(mItemsType);
        }
        mItemAdapter = new ItemAdapter(this, DialyList);
        mGridView_dialy.setAdapter(mItemAdapter);

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ImageView weather_iv=findViewById(R.id.iv_weather);
                mItemAdapter.selectPos=i;
                mItemAdapter.notifyDataSetInvalidated();//提示绘制发生了变化

                int simageId= provinceBeanList.get(i).getPictureID();

                weather_iv.setImageResource(simageId);
                id_weather=simageId;
            }
        });

        mGridView_mood.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ImageView weather_iv=findViewById(R.id.iv_mood);
                mItemAdapter.selectPos=i;
                mItemAdapter.notifyDataSetInvalidated();//提示绘制发生了变化

                int simageId=MoodList.get(i).getPictureID();
                weather_iv.setImageResource(simageId);
                id_mood=simageId;
            }
        });

        mGridView_dialy.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int simageId = DialyList.get(i).getPictureID();
                for (int j = 0; j < mDialy.size(); j++) {
                    if (mDialy.get(j) == simageId)
                        return;
                }
                    mDialy.add(simageId);
                    ImageView newImg = new ImageView(AddActivity.this);
                    //设置想要的图片，相当于android:src="@drawable/image"
                    newImg.setImageResource(simageId);
                    //设置子控件在父容器中的位置布局，wrap_content,match_parent
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.WRAP_CONTENT,
                            ViewGroup.LayoutParams.WRAP_CONTENT);
                    // 也可以自己想要的宽度，参数（int width, int height）均表示px
                    // 如dp单位，首先获取屏幕的分辨率在求出密度，根据屏幕ppi=160时，1px=1dp
                    //则公式为 dp * ppi / 160 = px ——> dp * dendity = px
                    //如设置为48dp：1、获取屏幕的分辨率 2、求出density 3、设置
                    DisplayMetrics displayMetrics = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
                    float density = displayMetrics.density;
                    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                            (int) (48 * density),
                            (int) (48 * density));

                    //相当于android:layout_marginLeft="8dp"
                    params1.leftMargin = 8;

                    //addView(View child)，默认往已有的view后面添加，后插入，不设置布局params,默认wrap_content
                    //mLinearLayout_dialy.addView(newImg);

                    //addView(View child, LayoutParams params)，往已有的view后面添加，后插入,并设置布局
                    mLinearLayout_dialy.addView(newImg, params1);

                    //addView(View view,int index, LayoutParams params),在某个index处插入
                    //mLinearLayout_dialy.addView(newImg, 0, params1);
                }


        });
    }



}